﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SalaryMS
{
    public partial class SalaryEmpAddUI : Form
    {
        public SalaryEmpAddUI()
        {
            InitializeComponent();

            //Init
            txtEmpId.Enabled = false;
            btnEmpSearch.Enabled = false;
            txtSalary.Enabled = false;
            btnConfirm.Enabled = false;
            lblMessage.Hide();

            String sql = "SELECT CmpId,CmpName FROM Company";
            DataTable dt = DBSession.ExecuteDataTable(sql, null);
            cmbCmp.DataSource = dt;
            cmbCmp.DisplayMember = "CmpName";
            cmbCmp.ValueMember = "CmpId";
            if (cmbCmp.SelectedValue != null && cmbCmp.SelectedValue.ToString().Length > 0)
            {
                sql = "SELECT SalaryDate FROM SalaryDate WHERE CmpId='" + cmbCmp.SelectedValue.ToString() + "'";
            }
            else
            {
                sql = "SELECT SalaryDate FROM SalaryDate ";
            }
            DataTable dtSD = DBSession.ExecuteDataTable(sql, null);
            cmbSalaryDate.DataSource = dtSD;
            cmbSalaryDate.DisplayMember = "SalaryDate";
            cmbSalaryDate.ValueMember = "SalaryDate";

            lblEmpId.Text = "";
            lblEmpName.Text = "";
            lblBankAccount.Text = "";
            lblCmpId.Text = "";
            lblCmpName.Text = "";
            txtEmpId.Text = "";
            txtSalary.Text = "";
        }

        private void txtEmpId_Enter(object sender, EventArgs e)
        {
            lblMessage.Hide();
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            if (btnSetting.Text == "设定")
            {
                if (cmbCmp.SelectedValue != null && cmbCmp.SelectedValue.ToString().Length > 0
                    && cmbSalaryDate.SelectedValue != null && cmbSalaryDate.SelectedValue.ToString().Length > 0)
                {
                    cmbCmp.Enabled = false;
                    cmbSalaryDate.Enabled = false;
                    btnSetting.Text = "取消设定";

                    txtEmpId.Enabled = true;
                    btnEmpSearch.Enabled = true;
                    this.AcceptButton = btnEmpSearch;
                    txtEmpId.Focus();
                }
                else
                {
                    MessageBox.Show("企业名字和薪水月份不能为空！");
                }
            }
            else
            {
                //Init
                txtEmpId.Enabled = false;
                btnEmpSearch.Enabled = false;
                txtSalary.Enabled = false;
                btnConfirm.Enabled = false;
                lblMessage.Hide();

                cmbCmp.Enabled = true;
                cmbSalaryDate.Enabled = true;
                btnSetting.Text = "设定";
                this.AcceptButton = btnSetting;
                cmbCmp.Focus();

                lblEmpId.Text = "";
                lblEmpName.Text = "";
                lblBankAccount.Text = "";
                lblCmpId.Text = "";
                lblCmpName.Text = "";
                txtEmpId.Text = "";
                txtSalary.Text = "";
            }
        }

        private void btnEmpSearch_Click(object sender, EventArgs e)
        {
            String EmpId = txtEmpId.Text.Trim().ToString();
            if (EmpId.Length > 0)
            {
                String sql = "SELECT Emplyee.EmpId,Emplyee.EmpName,Emplyee.BankAccount,Company.CmpId,Company.CmpName FROM Emplyee "
                    + " LEFT JOIN Company ON Emplyee.CmpId = Company.CmpId "
                    + " WHERE Emplyee.EmpId='" + EmpId + "' AND Company.CmpId='"+cmbCmp.SelectedValue+"'";
                DataTable dt = DBSession.ExecuteDataTable(sql, null);
                if (dt != null && dt.Rows.Count > 0)
                {
                    txtSalary.Enabled = true;
                    btnConfirm.Enabled = true;
                    txtSalary.Focus();
                    this.AcceptButton = btnConfirm;
                    lblEmpId.Text = dt.Rows[0]["EmpId"].ToString();
                    lblEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                    lblBankAccount.Text = dt.Rows[0]["BankAccount"].ToString();
                    lblCmpId.Text = dt.Rows[0]["CmpId"].ToString();
                    lblCmpName.Text = dt.Rows[0]["CmpName"].ToString();
                    lblMessage.Hide();
                }
                else
                {
                    MessageBox.Show("选择企业找不到此员工，请检查你输入的员工ID号");
                    txtEmpId.Focus();
                }
            }
            else
            {
                MessageBox.Show("员工ID号不能为空");
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (txtSalary.Text.Trim().Length > 0)
            {
                Decimal menoy = 0;
                try
                {
                     menoy = Decimal.Round(Decimal.Parse("" + txtSalary.Text.Trim() + " "), 2);
                     String sql = " INSERT INTO Salary(SalaryDate,EmpId,EmpName,BankAccount,CmpId,CmpName,Money)"
                     + " VALUES('" + cmbSalaryDate.SelectedValue + "','" + lblEmpId.Text + "','" + lblEmpName.Text + "','" + lblBankAccount.Text + "','" + lblCmpId.Text + "','" + lblCmpName.Text + "','" + menoy.ToString("#0.00") +"')";
                     int num = DBSession.ExecuteNonQuery(sql, null);
                     if (num > 0)
                     {
                         lblMessage.Text = "保存成功，请继续输入下一位！";
                         lblMessage.Show();

                         lblEmpId.Text = "";
                         lblEmpName.Text = "";
                         lblBankAccount.Text = "";
                         lblCmpId.Text = "";
                         lblCmpName.Text = "";
                         txtEmpId.Text = "";
                         txtSalary.Text = "";
                         txtSalary.Enabled = false;
                         btnConfirm.Enabled = false;
                         txtEmpId.Focus();
                         this.AcceptButton = btnEmpSearch;
                     }
                     else
                     {
                         lblMessage.Text = "保存失败，请重新确认！";
                         lblMessage.Show();
                     }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("请输入带二位小数的数字！");
                }

            }
            else
            {
                MessageBox.Show("薪水金额不能为空!");
                txtSalary.Focus();
            }
        }

        private void cmbCmp_SelectedValueChanged(object sender, EventArgs e)
        {
            String sql = "";
            if (cmbCmp.SelectedValue != null && cmbCmp.SelectedValue.ToString().Length > 0)
            {
                sql = "SELECT SalaryDate FROM SalaryDate WHERE CmpId='" + cmbCmp.SelectedValue.ToString() + "'";
            }
            else
            {
                sql = "SELECT SalaryDate FROM SalaryDate ";
            }
            DataTable dtSD = DBSession.ExecuteDataTable(sql, null);
            cmbSalaryDate.DataSource = dtSD;
            cmbSalaryDate.DisplayMember = "SalaryDate";
            cmbSalaryDate.ValueMember = "SalaryDate";
        }
    }
}
